/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.utilities;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author rantipin
 */
public class XMLHelper {
    
    //private final static String FILE_NAME = "lib/adhesionbound.xml";
    private final static String FILE_NAME = "c:/adhesionbound.xml";
    private final static String XML_HEAD =  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
                                            "<!DOCTYPE adhesionbound [" +
                                            "<!ELEMENT adhesionbound (element)*>" + 
                                            "<!ELEMENT element (name, value)> " +
                                            "<!ELEMENT name (#PCDATA)> " +
                                            "<!ELEMENT value (#PCDATA)>]>";
    
    private XMLHelper() {    
    }
    
    public LinkedHashMap<String, Integer> getAdhesionValues() {
        LinkedHashMap<String, Integer> adhesionValues = new LinkedHashMap<String, Integer>();
        
        try {
            File xmlFile = new File(FILE_NAME);
            if (xmlFile.exists() && xmlFile.isFile()) {
                
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(true);
                factory.setIgnoringElementContentWhitespace(true);
                
                DocumentBuilder builder = factory.newDocumentBuilder();
                builder.setErrorHandler(new ErrorHandler() {
                    public void warning(SAXParseException exception) throws SAXException {
                        throw exception;
                    }
                    public void error(SAXParseException exception) throws SAXException {
                        throw exception;
                    }
                    public void fatalError(SAXParseException exception) throws SAXException {
                        throw exception;
                    }
                });
                Document doc = builder.parse(xmlFile);
                NodeList adhesions = doc.getDocumentElement().getChildNodes();
                
                for (int i = 0; i < adhesions.getLength(); i++) {
                    Element element = (Element)adhesions.item(i);
                    Text name = (Text)((Element)element.getFirstChild()).getFirstChild();
                    Text value = (Text)((Element)element.getLastChild()).getFirstChild();
                    
                    try {
                        adhesionValues.put(name.getData().trim(), Integer.parseInt(value.getData().trim()));
                    } catch (Exception ex) {
                        Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
        } 
                
        return adhesionValues;
    }
    
    public void saveAdhesion(String name, int value) {
        
    }
    
    public void deleteAdhesion(String name, int value) {
        
    }
    
    public static XMLHelper getInstance() {
        if (INSTANCE == null) {
            synchronized(XMLHelper.class) {
                if (INSTANCE == null) {
                    INSTANCE = new XMLHelper();
                }
            }
        }
        
        return INSTANCE;
    }
    
    private static volatile XMLHelper INSTANCE = null;
}
